home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / c1.zip / CONIO.C < prev    next >
Text File  |  1987-06-18  |  5KB  |  154 lines

  1.  
  2. /**
  3. *
  4. * This module defines the various console I/O functions.  They may
  5. * be called directly, using the names included here, or the header
  6. * file CONIO.H may be included so that more standard names may be
  7. * used.  This source module is provided so that users may customize
  8. * the console I/O functions, if desired.  Note that "cprintf" and
  9. * "cscanf" (included in MC.LIB) call the functions "putch", "getch",
  10. * and "ungetch".
  11. *
  12. **/
  13. #define BDOS_IN   7     /* input function for "getch" */
  14. #define BDOS_OUT  6     /* output function for "putch" */
  15. #define BDOS_CKS  11    /* check keyboard status for "kbhit" */
  16. #define BDOS_BKI  10    /* buffered keyboardd input for "cgets" */
  17. #define BDOS_PRT  9     /* print string for "cputs" */
  18.  
  19. static char pushback;   /* character save for "ungetch" */
  20.  
  21. /**
  22. *
  23. * name          getch -- get character from console
  24. *
  25. * synopsis      c = getch();
  26. *               char c;         input character
  27. *
  28. * description   This function obtains the next character typed at
  29. *               the console or, if one was pushed back via "ungetch",
  30. *               returns the previously pushed back character.
  31. *
  32. **/
  33. getch()
  34. {
  35. int c;
  36.  
  37. if (pushback != '\0')
  38.    {                    /* character was pushed back */
  39.    c = pushback;
  40.    pushback = '\0';
  41.    return(c);
  42.    }
  43. return(bdos(BDOS_IN, 0xFF) & 127);
  44. }
  45. /**
  46. *
  47. * name          putch -- send character directly to console
  48. *
  49. * synopsis      putch(c);
  50. *               char c;         character to be sent
  51. *
  52. * description   This function sends the specified character directly
  53. *               to the user's console.
  54. *
  55. **/
  56. putch(c)
  57. char c;
  58. {
  59. bdos(BDOS_OUT, c&127);
  60. return(c);
  61. }
  62. /**
  63. *
  64. * name          ungetch -- push character back to console
  65. *
  66. * synopsis      r = ungetch(c);
  67. *               int r;          return code
  68. *               char c;         character to be pushed back
  69. *
  70. * description   This function pushes the indicated character back
  71. *               on the console.  Only a single level of pushback is
  72. *               allowed.  The effect is to cause "getch" to return
  73. *               the pushed-back character the next time it is called.
  74. *
  75. * returns       r = -1 if character already pushed back
  76. *               = c otherwise
  77. *
  78. **/
  79. ungetch(c)
  80. char c;
  81. {
  82.  
  83. if (pushback != '\0') return(-1);
  84. pushback = c;
  85. return(c);
  86. }
  87. /**
  88. *
  89. * name          cgets -- get string directly from console
  90. *
  91. * synopsis      p = cgets(s);
  92. *               char *p;        pointer to result string
  93. *               char *s;        string buffer (first byte = count)
  94. *
  95. * description   This function obtains a string directly from the
  96. *               user's console.  This version uses the buffered
  97. *               keyboard input function supported by the BDOS, so
  98. *               that all of the line editing capabilities are available.
  99. *               The first byte of "s" must be initialized to contain
  100. *               the number of bytes, minus two, in "s".  The string
  101. *               pointer returned is "s+2", which contains the first
  102. *               byte of input data.  Note that "s[1]" will contain
  103. *               the number of characters in the string.  The carriage
  104. *               return (which the user at the console must type to
  105. *               terminate the operation) is replaced by a null byte.
  106. *
  107. * returns       p = pointer to string received
  108. *
  109. **/
  110. char *cgets(s)
  111. char *s;
  112. {
  113. char *p;
  114.  
  115. if (*s == 0) *s = 250;          /* do not allow zero byte count */
  116. bdos(BDOS_BKI, s);
  117. p = s+2;
  118. p[s[1]] = '\0';                 /* set terminating byte */
  119. return(p);
  120. }
  121. /**
  122. *
  123. * name          cputs -- send character string directly to console
  124. *
  125. * synopsis      cputs(s);
  126. *               char *s;        character string to be sent
  127. *
  128. * description   This function sends the specified string directly to
  129. *               the user's console.  The BDOS function for "print
  130. *               string" is used.  The function locates the terminating
  131. *               null byte, changes it to a '$' (the terminator
  132. *               required by the BDOS function), and then changes it
  133. *               back to the null byte before returning.  Thus, the
  134. *               string to be printed cannot itself contain a '$' and
  135. *               it cannot reside in read-only memory (ROM).
  136. *
  137. *               Note that a carriage return or linefeed is NOT appended
  138. *               by this function; they must be included in the string,
  139. *               if desired.
  140. *
  141. **/
  142. cputs(s)
  143. char *s;
  144. {
  145. char *p;
  146.  
  147. for (p = s; *p != '\0'; p++) ;          /* find string terminator */
  148. *p = '$';
  149. bdos(BDOS_PRT, s);
  150. *p = '\0';
  151. return;
  152. }
  153.  
  154.